//
//  zhuceViewController.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/7.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "zhuceViewController.h"
#import <BmobSDK/Bmob.h>

@interface zhuceViewController ()

- (IBAction)zhucecancel:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *zhuce;
@property (weak, nonatomic) IBOutlet UITextField *mima;
@property (weak, nonatomic) IBOutlet UITextField *mima2;
@property (weak, nonatomic) IBOutlet UITextField *mailadress;


- (IBAction)zhuceTJ:(id)sender; //注册后提交到后台数据库

- (IBAction)textFiledReturnEditing:(id)sender; //响应键盘按下return关闭键盘
- (IBAction)mima2FiledReturnEditing:(id)sender;//按下mima2检测两次输入密码是否一致

- (IBAction)backgroundTap:(id)sender; //点击背景退出键盘
@end

@implementation zhuceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)zhuceTJ:(id)sender {
    BmobUser *User = [[BmobUser alloc] init];
    [User setUsername:_zhuce.text];
    [User setPassword:_mima.text];
    [User setEmail:_mailadress.text];
    //进行textfield判断时候 默认是为nil的 不能进行直接判断最好用长度来判断
    if (!(_mailadress.text.length==0||_zhuce.text.length==0||_mima.text.length==0||_mima2.text.length==0)){
    [User signUpInBackgroundWithBlock:^ (BOOL isSuccessful, NSError *error){
        if (isSuccessful){
            NSLog(@"Sign up successfully");

            BmobObject  *gameScore = [BmobObject objectWithClassName:_zhuce.text];
            //设置Ename
            [gameScore setObject:@"小明" forKey:@"Ename"];
            //设置表的各项属性
            [gameScore setObject:@"tj" forKey:@"city"];
            [gameScore setObject:@"tj" forKey:@"call"];
            [gameScore setObject:@"tj" forKey:@"adress"];
            [gameScore setObject:@"tj" forKey:@"email"];
            [gameScore setObject:@"tj" forKey:@"lawman"];
            [gameScore setObject:@"tj" forKey:@"setmoney"];
            [gameScore setObject:@"tj" forKey:@"setdate"];
            //异步保存到服务器
            [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                   
                    //将第一条数据删除 ，形成一个空表
                    [gameScore deleteInBackground];
                    
                    NSLog(@"%@",gameScore);
                } else if (error){
                    //发生错误后的动作
                    NSLog(@"%@",error);
                }
            }];
            
          
        //像注册用户邮箱发送验证邮件
            [BmobUser loginWithUsernameInBackground:_zhuce.text
                                           password:_mima.text
                                              block:^(BmobUser *user,NSError *error){
                                                  if (user) {
                                                     // BmobUser *user = [BmobUser getCurrentUser];
                                
                                                [user verifyEmailInBackgroundWithEmailAddress:_mailadress.text];
                                                          
                                                 [BmobUser logout];     
                                                }
                                                  else {
                                                      NSLog(@"%@",error);
                                                                         }}];
            UIAlertController *sucinfo = [UIAlertController
                                          alertControllerWithTitle:@"提示"
                                          message:@"注册成功，请进入邮箱验证邮件"
                                          preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction *action){
                                                                  [self dismissViewControllerAnimated:YES completion:nil];
                                                              }];
            [sucinfo addAction:yesAction];
            //显示
            [self presentViewController:sucinfo animated:true completion:nil];
            
            
        }
        else {
            NSLog(@"%@",error);
            UIAlertController *zhuceYZ = [UIAlertController
                                                 alertControllerWithTitle:@"错误"
                                                 message:@"账号已存在或不符合要求"
                                                 preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction *action){
                                                              }];
            [zhuceYZ addAction:yesAction];
            //显示
            [self presentViewController:zhuceYZ animated:true completion:nil];
        }
    }];
}
  else{
      UIAlertController *infoWZ = [UIAlertController
                                    alertControllerWithTitle:@"错误"
                                    message:@"请完善注册信息，尚有未完成部分"
                                    preferredStyle:UIAlertControllerStyleAlert];
      
      UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                          style:UIAlertActionStyleCancel
                                                        handler:^(UIAlertAction *action){
                                                        }];
      [infoWZ addAction:yesAction];
      //显示
      [self presentViewController:infoWZ animated:true completion:nil];
  }
    
}

-(IBAction)textFiledReturnEditing:(id)sender {
    [sender resignFirstResponder];
}

- (IBAction)mima2FiledReturnEditing:(id)sender{
    if (_mima.text==_mima2.text) {
        [sender resignFirstResponder];
    } else {
        UIAlertController *mimaYZ = [UIAlertController
                                      alertControllerWithTitle:@"错误"
                                      message:@"两次密码输入不一致"
                                      preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction *action){
                                                          }];
        [mimaYZ addAction:yesAction];
        //显示
        [self presentViewController:mimaYZ animated:true completion:nil];
    }

}

- (IBAction)backgroundTap:(id)sender {
    [_mailadress resignFirstResponder];
    [_zhuce resignFirstResponder];
    [_mima resignFirstResponder];
    [_mima2 resignFirstResponder];
}

- (IBAction)zhucecancel:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
